//
//  MTChatSingleDetailCell.swift
//  MT
//
//  Created by ooops on 2020/5/7.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

protocol MTChatSingleDetailCellDelegate: NSObjectProtocol {
    func didOpenSwitch(sender: UISwitch, data: ChatItemStruct?)
}

class MTChatSingleDetailCell: UITableViewCell {
    
    private var data: ChatItemStruct?
    
    var conversation: JMSGConversation!
    
    var user: JMSGUser!
    
    weak var delegate: MTChatSingleDetailCellDelegate?
    
    lazy var actionItemNameLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(14)
        l.textColor = UIColor.text.x1d1a1a
        return l
    }()
    
    lazy var arrowIconImageView: UIImageView = {
        let imageV = UIImageView()
        imageV.image = UIImage(named: "icon_arrow_right_gary")
        imageV.isHidden = true
        return imageV
    }()
    
    lazy var switchButton: UISwitch = {
        let s = UISwitch()
        s.isOn = false
        s.isHidden = true
        s.addTarget(self, action: #selector(switchOnOff(sender:)), for: .touchUpInside)
        return s
    }()
    
    lazy var lineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex: 0xE1E1E1)
        return v
    }()
    
    func bindData(data: ChatItemStruct) {
        actionItemNameLabel.text = data.itemName
        arrowIconImageView.isHidden = !data.isShowArrow
        if data.isShowArrow == false {
            switchButton.isHidden = !data.isShowSwitch
            if data.isShowSwitch {
                arrowIconImageView.isHidden = true
            }
        }
        
        if data.type == .roofTop {
            switchButton.isOn =  conversation.ex.isSticky
        }
        
        if data.type == .unDisturb {
            switchButton.isOn = user.isNoDisturb
        }
        
        
        self.data = data
    }
    
    @objc private func switchOnOff(sender: UISwitch) {
        self.delegate?.didOpenSwitch(sender: sender, data: self.data)
    }
    
    class func reuseIdentity() -> String {
        return "MTChatSingleDetailCellID"
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        makeConstraint()
        selectionStyle = .none
    }
    
    private func setupUI() {
        contentView.addSubview(actionItemNameLabel)
        contentView.addSubview(arrowIconImageView)
        contentView.addSubview(switchButton)
        contentView.addSubview(lineView)
    }
    
    private func makeConstraint() {
        actionItemNameLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(10)
        }
        
        arrowIconImageView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-10)
        }
        
        switchButton.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-10)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
